from django.db import models
from goods.models import Good
from users.models import User


# 优惠类型
class CouponType(models.Model):
    type_name = models.CharField('优惠类型名称', max_length=30,)

    def __str__(self):
        return self.type_name

    class Meta:
        db_table = 'coupon_type'
        verbose_name_plural = "优惠券类型"


# 优惠券
class Coupon(models.Model):
    name = models.CharField('优惠券名称', max_length=100)
    brief = models.TextField('优惠券介绍', default='')
    coupon_type = models.ForeignKey(CouponType, on_delete=models.CASCADE,
                                    verbose_name='优惠券类型', null=True, blank=True)
    good = models.ForeignKey(Good, on_delete=models.CASCADE, verbose_name='单品消费', null=True, blank=True)
    coupon_num = models.IntegerField('数量', default=0)
    start_time = models.DateTimeField('开始使用时间')
    over_time = models.DateTimeField('结束使用时间')
    start_get_time = models.DateTimeField('领券开始时间')
    end_get_time = models.DateTimeField('领券结束时间')
    remark = models.CharField('备注', max_length=300)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'coupon'
        verbose_name_plural = "优惠券"

# 优惠券 --类型 --具体的值
class CouponValue(models.Model):
    coupon = models.ForeignKey(Coupon, on_delete=models.CASCADE, verbose_name='优惠券')
    coupon_type = models.ForeignKey(CouponType, on_delete=models.CASCADE, verbose_name='优惠券类型')
    value = models.IntegerField('类型具体的值', default=0)

    def __str__(self):
        return '%s:%s:%s' % (self.coupon.name, self.coupon_type.type_name,self.value)

    class Meta:
        db_table = 'coupon_value'

# 用户领券表
class GetCoupon(models.Model):
    COUPON_STATUS = (
        (0, '未使用'),
        (1, '已使用'),
        (2, '已过期'),
    )
    coupon = models.ForeignKey(Coupon, on_delete=models.CASCADE, verbose_name='优惠券')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')
    status = models.SmallIntegerField('券的使用状态', choices=COUPON_STATUS, default=0)
    get_time = models.DateTimeField('领券时间', auto_now_add=True)
    use_time = models.DateTimeField('领券时间', null=True, blank=True)

    def __str__(self):
        return '%s:%s-%s' % (self.user.username, self.coupon.name, self.get_status_display())

    class Meta:
        db_table = 'user_get_coupon'